package com.neusoft.elm_servlet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm_servlet.dao.CartDao;
import com.neusoft.elm_servlet.po.Business;
import com.neusoft.elm_servlet.po.Cart;
import com.neusoft.elm_servlet.po.Food;
import com.neusoft.elm_servlet.util.DBUtil;

public class CartDaoImpl implements CartDao {

	@Override
	public List<Cart> listCart(String userId, String businessId) throws SQLException {
		Connection con = DBUtil.getConnection();
		StringBuffer sql = new StringBuffer("SELECT * FROM cart c,food f,business b WHERE c.foodId=f.foodId AND c.businessId=b.businessId AND c.userId = ? ");
		if (businessId != null && businessId !="") {
			sql.append(" AND c.businessId=?");
		}
		PreparedStatement ps = con.prepareStatement(sql.toString());
		ps.setString(1, userId);
		if (businessId != null && businessId !="") {
			ps.setString(2, businessId);
		}
		ResultSet rs = ps.executeQuery();
		List<Cart> list = new ArrayList<>();
		while(rs.next()){
			Cart cart = new Cart();
			cart.setCartId(rs.getInt("cartId"));
			cart.setQuantity(rs.getInt("quantity"));
			Food food = new Food();
			food.setFoodId(rs.getInt("foodId"));
			food.setFoodImg(rs.getString("foodImg"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodPrice(rs.getDouble("foodPrice"));
			food.setFoodExplain(rs.getString("foodExplain"));
			Business business = new Business();
			business.setBusinessId(rs.getInt("businessId"));
			business.setBusinessName(rs.getString("businessName"));
			business.setBusinessAddress(rs.getString("businessAddress"));
			business.setBusinessExplain(rs.getString("businessExplain"));
			business.setBusinessImg(rs.getString("businessImg"));
			business.setOrderTypeId(rs.getInt("orderTypeId"));
			business.setStarPrice(rs.getDouble("starPrice"));
			business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
			business.setRemarks(rs.getString("remarks"));
			cart.setFood(food);
			cart.setBusiness(business);
			list.add(cart);
		}
		DBUtil.close(rs, ps);
		return list;
	}

	@Override
	public int saveCart(Cart cart) throws SQLException {
		Connection con = DBUtil.getConnection();
		//prepareStatement 增加获取自增长key的标识
		//empno为自增长，赋值null即可
		PreparedStatement ps = con.prepareStatement("insert into"
				+ " cart values(null,?,?,?,1)" );
		//放参数
		ps.setInt(1, cart.getFoodId());
		ps.setInt(2, cart.getBusinessId());
		ps.setString(3, cart.getUserId());
		//获取受影响的行数
		int count = ps.executeUpdate();
		//调用关流方法 此时没有rs，所以定义为null
		DBUtil.close(null, ps, con);
		
		return count;
	}

	@Override
	public int updateCart(Cart cart) throws SQLException {
		Connection con = DBUtil.getConnection();
		//prepareStatement 增加获取自增长key的标识
		//empno为自增长，赋值null即可
		PreparedStatement ps = con.prepareStatement("update cart set "
				+ " quantity=? where foodId=? and businessId=? and userId=? " );
		//放参数
		ps.setInt(1, cart.getQuantity());
		ps.setInt(2, cart.getFoodId());
		ps.setInt(3, cart.getBusinessId());
		ps.setString(4, cart.getUserId());
		//获取受影响的行数
		int count = ps.executeUpdate();
		//调用关流方法 此时没有rs，所以定义为null
		DBUtil.close(null, ps, con);
		
		return count;
	}

	@Override
	public int removeCart(Cart cart) throws SQLException {
		Connection con = DBUtil.getConnection();
		StringBuffer sql = new StringBuffer("delete from cart where businessId=? and userId=? ");
		if (cart.getFoodId() != null && cart.getFoodId() != 0) {
			sql.append(" AND foodId=?");
		}
		PreparedStatement ps = con.prepareStatement(sql.toString());
		//放参数
		ps.setInt(1, cart.getBusinessId());
		ps.setString(2, cart.getUserId());
		if (cart.getFoodId() != null && cart.getFoodId() != 0) {
			ps.setInt(3, cart.getFoodId());
		}
		//获取受影响的行数
		int count = ps.executeUpdate();
		//调用关流方法 此时没有rs，所以定义为null
		DBUtil.close(null, ps);
		
		return count;
	}

}
